using System;
using System.Collections;
using System.Collections.Generic;
using CommunityServer.Blogs.Components;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Components.SearchBarrel;

namespace CommunityServerStuff.SearchBarrel
{
    class ContentSearchJob : SearchJob
    {

        #region Constructor

        public ContentSearchJob()
        {
        }

        #endregion

        public override void IndexPosts(int setSize, int settingsID)
        {

           ArrayList contentSet = (ArrayList)Contents.GetAllContent();
            
            foreach (Content post in contentSet) 
            {

                Hashtable words = new Hashtable();
                int totalBodyWords = 0;

                if(post != null)
                {

                    // Process the post subject
                    //
                    words = Index(post.Title, words, WordLocation.Subject, settingsID);

                    // Process the post body
                    //
                    words = Index(post.Body, words, WordLocation.Body, settingsID);

                    // Get a count of the total words in the body
                    //
                    totalBodyWords = SearchTerms.CleanSearchTerms(post.Body).Length;
                    IContent newpost = post as IContent;
                    Post reallynewpost = newpost as Post;
                    InsertIntoSearchBarrel(words, reallynewpost, settingsID, totalBodyWords);
                }

            }
        }

        protected override double CalculateWordRank(Word word, Post post, int totalBodyWords)
        {
            int wordOccurrence = 0;
            double wordCount = 0;

            // Word weighting:
            // ============================
            // Word Occurence:      20
            // Replies:             20
            // Total words in post: 20
            // Locked:               5
            // Pinned:               5
            // Word in Excerpt:      5
            // Word in Subject:     10
            //                    ----
            // Max score:           85

            // Assign a score for how many times the word occurs in the post
            //
            wordOccurrence = word.Occurence * 2;
            if (wordOccurrence > 20)
                wordOccurrence = 20;

            // Calculate a score for the count of total words in the post
            //
            if (totalBodyWords > 65)
                wordCount = 20;
            else
                wordCount = (totalBodyWords / 65f) * 20f;

            // Calculate the final weight of the word
            //
            return (wordOccurrence + word.OccurenceWeight + wordCount) / 85f;
        }

	}
}
